package com.kevineric;

import java.io.File;
import java.io.PrintStream;
import java.net.Socket;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

public class Mouse extends Activity {

	private SensorManager mSensorManager;

	int count = 0;

	int size = 10;
	int centerX = 150;
	int centerY = 250;
	
	float myX = 0;
	float myY = 0;
	
	int mag = 1;
	
	Panel panel;
    
    Socket sock;
    PrintStream sockout;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.main);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        panel = new Panel(this);
        setContentView(panel);

		// Set up the accelerometer reading
		mSensorManager = (SensorManager)getSystemService
		(Context.SENSOR_SERVICE);
		// Get the list of all sensors, and find the accelerometer within
		List<Sensor> sensorList = mSensorManager.getSensorList
		(SensorManager.SENSOR_ACCELEROMETER);

		mSensorManager.registerListener(mSensorListener,
				sensorList.get(0),
				SensorManager.SENSOR_DELAY_FASTEST);
		
		try{
			sock=new Socket("192.168.1.101", 4055);
			sockout=new PrintStream(sock.getOutputStream());
			Log.v("acceldata", "connection established");
		}catch(Exception e){
			Log.v("acceldata", "Error opening socket"+e.toString());
		}

	}

	
	//Accelerometer
	private final SensorEventListener mSensorListener = new
	SensorEventListener() {

		private PrintStream mCurrentFile;

		boolean never = true;
		String comma = new String(",");

		public void runOnce()
		{
			//Creating a file to print the data into

			String nameStr = new String("/sdcard/10 second trials.xls");
			File outputFile = new File(nameStr);
			mCurrentFile = null;

				mCurrentFile = System.out;


		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		private float[] prevValues=new float[3];

		Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(b);
		
		
		boolean parity = true;
		
		int ignore = 10;
		int wait = 10;
		float waitsumX = 0;
		float waitsumY = 0;
		
		float biasX = 0;
		float biasY = 0;
		
		@Override
		public void onSensorChanged(SensorEvent event) {
			
			if (never)
			{
				never = false;
				runOnce();
			}
			count++;
			
			
			boolean doPrint=false;
			
			float[] diffs=new float[3];
			for(int i=0; i<3; i++){
				diffs[i]=event.values[i]-prevValues[i];
				if(  Math.abs(diffs[i])>2.0){
					doPrint=true;
				}
				prevValues[i]=event.values[i];
			}

			if(count < ignore) {
				Log.v("acceldata", " " + count);
				return;
			}

			if(ignore < count && count <= ignore + wait) {
				waitsumX += event.values[0];
				waitsumY += event.values[1];
				return;
			}
			
			if(count == ignore + wait + 1) {
				myX = 0;
				myY = 0;
				biasX = waitsumX / wait;
				biasY = waitsumY / wait;
			}
			
			float tempX = event.values[0] - biasX;
			float tempY = event.values[1] - biasY;
			float tol = 0;
			
			myX += Math.abs(tempX) < tol ? 0 : tempX;
			myY += Math.abs(tempY) < tol ? 0 : tempY;
			
			/*
			if( count % 5==0){
				try{
					Log.v("acceldata", "biasX: " + biasX + ", biasY: " + biasY + "event.values[1]: " + event.values[1]);
					sockout.println("m,"+((int)(myX*mag))+","+((int)(-myY*mag)));
				}catch(Exception e){
					Log.v("acceldata", "connection failed "+e.toString());
				}
			}*/
			
			
			//Log.e("acceldata", myX + " " + myY);
			
			StringBuffer buff = new StringBuffer();
			buff.append(",");
			buff.append(String.valueOf(event.values[0]));
			buff.append(",");
			buff.append(String.valueOf(event.values[1]));
			buff.append(",");
			buff.append(String.valueOf(event.values[2]));
			//if(doPrint)
			Log.e("acceldata", buff.toString());
		}
	};
	
    class Panel extends View {
        public Panel(Context context) {
            super(context);
        }
 
        
        @Override
        public void onDraw(Canvas canvas) {
            //Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
            canvas.drawColor(Color.BLACK);
            Paint p = new Paint();
            p.setARGB(255, 255, 0, 0);
            
          //  canvas.drawRect(r, p);
        }
	}
	
}
